package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 根据ID获取公寓信息详情
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo getApartemntDetailById(Long id) {
        //根据ID获取公寓基础信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) return null;
        //获取公寓图片列表
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectByItemTypeAndId(ItemType.APARTMENT, id);
        //获取标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectApartmentLabelList(id);
        //获取配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectApartmentFacililtyList(id);
        //获取租金最小值
        BigDecimal minRent = roomInfoMapper.selectMinRentById(id);
        //获取公寓信息是否被删除
        Boolean isDelete = apartmentInfo.getIsDeleted() == 1;
        //查询的信息存入实体类中
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(apartmentGraphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(isDelete);
        return apartmentDetailVo;
    }

    //根据ID获取公寓端信息
    @Override
    public ApartmentItemVo selectApartmentItemVoById(Long apartmentId) {
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        List<GraphVo> graphVoList = graphInfoMapper.selectByItemTypeAndId(ItemType.APARTMENT, apartmentId);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectApartmentLabelList(apartmentId);
        BigDecimal minRent = roomInfoMapper.selectMinRentById(apartmentId);
        //存入实体类中
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);
        return apartmentItemVo;
    }
}




